package controllers

import (
	"github.com/milkbobo/fishgoweb/language"
	"miners-monitor/models/client"
	"miners-monitor/models/common"
	"miners-monitor/models/rewardBurn"
	"miners-monitor/models/total"
)

type IndexController struct {
	BaseController
	CommonFunc       common.CommonFunc
	TotalAo          total.TotalAoModel
	DefineController DefineController
	ClientAo         client.ClientAoModel
}

func (this *IndexController) Get_Json() interface{} {
	minerId := this.Ctx.GetParam("MinerId")
	this.ClientAo.IsLoginAndCheckoutAuth(minerId)
	return this.DefineController.GetMinerTotalByMinerId(minerId)
}
func (this *IndexController) AgainSyncOldData_Json() {
	this.DefineController.AgainSyncOldData()
}

func (this *IndexController) GetTotal_Json() interface{} {
	return this.DefineController.GetTotal()
}

func (this *IndexController) SyncExpend_Json() {
	this.ClientAo.IsLoginAndCheckoutAuth(client.ClientAuthorityEnum.MinersManagement)
	go func() {
		defer language.CatchCrash(func(exception language.Exception) {
			this.Log.Critical("SyncExpend_Json Crash Code:[%d] Message:[%s]\nStackTrace:[%s]", exception.GetCode(), exception.GetMessage(), exception.GetStackTrace())
		})
		this.Log.Debug("SyncExpend_Json:%+v", "SyncExpend_Json")
		this.DefineController.SyncExpend()
	}()
}

func (this *IndexController) SyncIncome_Json() {
	this.ClientAo.IsLoginAndCheckoutAuth(client.ClientAuthorityEnum.MinersManagement)
	go func() {
		defer language.CatchCrash(func(exception language.Exception) {
			this.Log.Critical("SyncIncome Crash Code:[%d] Message:[%s]\nStackTrace:[%s]", exception.GetCode(), exception.GetMessage(), exception.GetStackTrace())
		})
		this.Log.Debug("SyncIncome:%+v", "SyncIncome")
		this.DefineController.SyncIncome()
	}()

}

func (this *IndexController) SyncYesterday_Json() {
	this.ClientAo.IsLoginAndCheckoutAuth(client.ClientAuthorityEnum.MinersManagement)
	go func() {
		defer language.CatchCrash(func(exception language.Exception) {
			this.Log.Critical("SyncYesterday Crash Code:[%d] Message:[%s]\nStackTrace:[%s]", exception.GetCode(), exception.GetMessage(), exception.GetStackTrace())
		})
		this.Log.Debug("SyncYesterday:%+v", "SyncYesterday")
		this.DefineController.SyncYesterday()
	}()

}

func (this *IndexController) SyncState_Json() interface{} {

	return struct {
		SyncTotalMinersByHeight map[string]int64
		SyncMsgMinerMap         map[string]int64
		SyncRewardMinerMap      map[string]int64
		SyncBlockMinerMap       map[string]int64
		DoneSyncVestingFunds    int64
	}{
		SyncTotalMinersByHeight: SyncTotalMinersByHeight,
		SyncMsgMinerMap:         rewardBurn.SyncMsgMinerMap,
		SyncRewardMinerMap:      rewardBurn.SyncRewardMinerMap,
		SyncBlockMinerMap:       rewardBurn.SyncBlockMinerMap,
		DoneSyncVestingFunds:    total.SyncVestingFundsByHeight,
	}

}
